//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using SR = Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using Microsoft.Research.ScientificWorkflow.UIDesigner;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    /// <summary>
    /// Interaction logic for PropertyPanePopup.xaml
    /// </summary>
    public sealed partial class PropertyPanePopup : Window
    {
        /// <summary>
        /// WorkflowModel to be displayed.
        /// </summary>
        private TridentWorkflowModel wfModel;

        /// <summary>
        /// The base model for which the properties are to be shown.
        /// </summary>
        private BaseModel model;

        /// <summary>
        /// Properties pane.
        /// </summary>
        private PropertiesPane propPane;

        /// <summary>
        /// The registry connection for accessing the categories.
        /// </summary>
        private SR.Connection registryConn;

        /// <summary>
        /// Should the activity properties be shown or not.
        /// </summary>
        private bool showActivityProperties;

        /// <summary>
        /// Specifies if only the errors are to be shown.
        /// </summary>
        private bool showOnlyErrors;

        /// <summary>
        /// Invoked by Save As or not
        /// </summary>
        private bool invokedBySaveAs;

        /// <summary>
        /// PropertyPane popup.
        /// </summary>
        /// <param name="conn">
        /// Registry connection.
        /// </param>
        /// <param name="model">
        /// Base model.
        /// </param>
        public PropertyPanePopup(SR.Connection connection, BaseModel model, bool showOnlyErrors)
        {
            InitializeComponent();
            this.registryConn = connection;
            this.model = model;
            this.showOnlyErrors = showOnlyErrors;
            this.showActivityProperties = true;
            this.Loaded += new RoutedEventHandler(this.OnLoaded);
            if (!showActivityProperties)
            {
                this.brdrPropPaneHolder.MinHeight = 0;
                this.MinHeight = 0;
                this.MinWidth = 0;
                this.MaxWidth = double.PositiveInfinity;
                this.MaxHeight = double.PositiveInfinity;

                this.brdrPropPaneHolder.MaxHeight = 700;
                this.brdrPropPaneHolder.MaxWidth = 400;
                this.ResizeMode = ResizeMode.NoResize;
            }
        }

        /// <summary>
        /// PropertyPane popup.
        /// </summary>
        /// <param name="conn">
        /// Registry connection.
        /// </param>
        /// <param name="wfModel">
        /// WorkflowModel.
        /// </param>
        public PropertyPanePopup(SR.Connection connection, TridentWorkflowModel workflowModel, bool showActivityProperties)
        {
            InitializeComponent();
            this.wfModel = workflowModel;
            this.registryConn = connection;
            this.showActivityProperties = showActivityProperties;
            this.Loaded += new RoutedEventHandler(this.OnLoaded);
            if (!showActivityProperties)
            {
                this.brdrPropPaneHolder.MinHeight = 0;
                this.MinHeight = 0;
                this.MinWidth = 0;
                this.MaxWidth = double.PositiveInfinity;
                this.MaxHeight = double.PositiveInfinity;

                this.brdrPropPaneHolder.MaxHeight = 700;
                this.brdrPropPaneHolder.MaxWidth = 400;
                this.ResizeMode = ResizeMode.NoResize;
            }
        }

        /// <summary>
        /// PropertyPane popup.
        /// </summary>
        /// <param name="conn">
        /// Registry connection.
        /// </param>
        /// <param name="wfModel">
        /// WorkflowModel.
        /// </param>
        public PropertyPanePopup(SR.Connection connection, TridentWorkflowModel workflowModel, bool showActivityProperties, bool isOpenedFromSaveAs)
            : this(connection,workflowModel,showActivityProperties)
        {
            this.invokedBySaveAs = isOpenedFromSaveAs;
            this.SizeToContent = SizeToContent.Manual;
        }

        /// <summary>
        /// Get and set the flag if the user cancel or saved the workflow
        /// </summary>
        public bool ReadyToSave { get; set; }

        public SR.User User { get; set; }

        /// <summary>
        /// Fired on loaded.
        /// </summary>
        /// <param name="sender">
        /// This.
        /// </param>
        /// <param name="e">
        /// RoutedEventArgs.
        /// </param>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (showOnlyErrors)
            {
                propPane = new PropertiesPane(this.registryConn, this.model, PropertyPaneMode.Popup, true);
            }
            else
            {
                propPane = new PropertiesPane(this.registryConn, this.wfModel, PropertyPaneMode.Popup, false, this.showActivityProperties, this.invokedBySaveAs);
            }

            if (null == this.User)
            {
                propPane.User = TridentAuthentication.LoggedInUser;
            }
            else
            {
                propPane.User = this.User;
            }

            propPane.OnPropPaneDrag += new EventHandler<MouseButtonEventArgs>(this.OnPaneDrag);
            propPane.ClosePropPane += new EventHandler(this.OnClose);
            propPane.OnSaveClicked += new EventHandler(this.OnOkClicked);
            propPane.Loaded += new RoutedEventHandler(propPane_Loaded);
            this.brdrPropPaneHolder.Child = propPane;

        }

        void propPane_Loaded(object sender, RoutedEventArgs e)
        {
            this.Height = this.ActualHeight;
            this.Width = this.ActualWidth;
        }

        /// <summary>
        /// Called when pane is dragged.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnPaneDrag(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// Fires when Property Pane Ok clicked
        /// </summary>
        /// <param name="sender">PropPane</param>
        /// <param name="e">EventArgs</param>
        private void OnOkClicked(object sender, EventArgs e)
        {
            this.ReadyToSave = true;
            this.Close();
        }

        /// <summary>
        /// On close.
        /// </summary>
        /// <param name="sender">
        /// PropPane.
        /// </param>
        /// <param name="e">
        /// EventArgs.
        /// </param>
        private void OnClose(object sender, EventArgs e)
        {
            this.ReadyToSave = false;
            this.Close();
        }
    }
}
